package com.dycong.common.leetcode.ds;

/**
 * @author dycong
 * @date 2019/9/14 12:27
 */
public class LinkedNode<T> {

    public T val;
    public LinkedNode<T> next;

    public LinkedNode(T val) {
        this.val = val;
    }


    public static String format(LinkedNode head) {
        LinkedNode pre = head;
        StringBuilder builder = new StringBuilder();
        while (pre != null) {
            builder.append(pre.val)
                    .append(",");
            pre = pre.next;
        }
        return builder.toString();
    }


    public static LinkedNode<Integer> generate(int length) {
        LinkedNode<Integer> head = new LinkedNode<>(0);
        LinkedNode<Integer> cur = head;
        for (int i = 1; i < length; i++) {
            cur.next = new LinkedNode<>(i);
            cur = cur.next;
        }
        return head;
    }
}
